package org.gwt.grin.client.input;

import org.gwt.grin.client.Segment;
import org.gwt.grin.client.Show;
import org.gwt.grin.client.commands.Command;

/**
 * 
 * @author ggeorg
 */
public class CommandRCHandler extends RCHandler {
	
	protected int mask;
	protected Command[] commands;
	protected boolean wantKeypress; // otherwise wants key release
	
	public CommandRCHandler(String name, int mask, boolean wanKeypress) {
		super();
		this.name = name;
		this.mask = mask;
		this.wantKeypress = wanKeypress;
	}
	
	public void setup(Command[] commands) {
		this.commands = commands;
	}

	@Override
	public boolean handleKeyPressed(RCKeyEvent ke, Show caller) {
		if(wantKeypress && (ke.getBitMask() & mask) != 0){
			for(Command c : commands) {
				c.execute(caller);
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean handleKeyReleased(RCKeyEvent ke, Show caller) {
		if(!wantKeypress && (ke.getBitMask() & mask) != 0){
			for(Command c : commands) {
				c.execute(caller);
			}
			return true;
		}
		return false;
	}
	
	@Override
	public boolean handleMouse(int x, int y, boolean activate) {
		return false;
	}

	@Override
	public void activate(Segment s) {
		// do nothing
	}

	@Override
	public void nextFrame() {
		// do nothing
	}

}
